package com.easemob.chatui.adapter;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.json.Task;
import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class TaskAdapter extends BaseAdapter{

    private List<Task> taskList;
    private LayoutInflater mInflater = null;
    public TaskAdapter(Context context, List<Task> taskList) {
        this.taskList = taskList;
        this.mInflater = LayoutInflater.from(context);
    }

    public void update(List<Task> list) {
        this.taskList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (taskList == null) {
            return 0;
        }
        return taskList.size();
    }

    @Override
    public Object getItem(int i) {
        if (taskList == null) {
            return null;
        }
        return taskList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        //如果缓存convertView为空，则需要创建View
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.task_list_item, null);
            holder.title = (TextView) convertView.findViewById(R.id.text_title);
            holder.time = (TextView) convertView.findViewById(R.id.text_time);
            holder.state = (TextView) convertView.findViewById(R.id.task_state);
            convertView.setTag(holder);
        }else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        Task task = (Task) getItem(i);
        if (task == null) {
            return null;
        }
        holder.title.setText(task.getTitle());

        SimpleDateFormat formatter
                = new SimpleDateFormat(viewGroup.getContext().getString(R.string.time_format), Locale.CHINA);
        Date curDate = new Date(task.getStart());//获取当前时间
        String time = formatter.format(curDate);
        holder.time.setText(time);

        if (task.isDone()) {
            holder.state.setText(R.string.is_done);
            holder.state.setEnabled(false);
        } else {
            holder.state.setText(R.string.doing);
        }
        return convertView;
    }

    public static class ViewHolder {

        public TextView title;
        public TextView time;
        public TextView state;

    }

}
